<?php 

	//数据库相关配置	
	define('HOST','localhost');
	define('USER','root');
	define('PASS','');
	define('DBNAME','lamp154');
	define('CHARSET','utf8');

	//封装Model类
	class Model
	{
		//成员属性
		public $tableName;//表名
		public $link;//链接标识
		public $pk;//主键
		public $fields;//普通字段
		public $where;//where 条件
		public $limit;//limit 条件
		public $field;//field 条件

		//构造方法
		public function __construct($name)
		{
			//初始化
			$this->tableName = $name;
			// 1,链接数据库
			$this->link = mysqli_connect(HOST,USER,PASS) or die('数据库链接失败');
			// 2,选择库
			mysqli_select_db($this->link,DBNAME) or die('数据库选中失败');
			//3,设置字符集
			mysqli_set_charset($this->link,CHARSET) or die('字符集失败');
		
			//实例化对象就去获取当前表的字段信息
			$this->getFileds();
		}

		//进行当前表字段和主键的获取
		public function getFileds()
		{
			//sql
			$sql = 'desc '.$this->tableName;
			//执行
			$res = mysqli_query($this->link,$sql);
			//判断
			if($res){
				//普通字段
				$arr = [];
				//主键
				$pk = '';
				while($row = mysqli_fetch_assoc($res)){
					//判断是否为主键
					if($row['Key'] == 'PRI'){
						$pk = $row['Field'];
					}else{
						$arr[] = $row['Field'];
					}
				}
				//把字段信息存入属性中
				$this->pk = $pk;
				$this->fields = $arr;
			}else{
				return false;
			}
		}

		//添加操作 insert
		/*
			参数 关联数组
				['username'=>'zhangsan','password'=>'zhangsans','email'=>'zhangsan@qq.com']
				username,password,email   'zhangsan','zhansgans','@qq.com'
			返回值:
				成功 返回id
				失败 false
		*/

		public function insert(array $arr)
		{
			// var_dump($arr);die;
			//检测是否有非法字段
			foreach($arr as $k=>$v){
				//检测
				if(!in_array($k,$this->fields)){
					//不存在
					// unset($arr[$k]);
					die('您给的数据含有非法字段:'.$k.',请配合安检');
				}
			}
			
			//获取字段
			$keys = array_keys($arr);
			$ksql = implode(',',$keys);
			//获取值
			$values = array_values($arr);
			$vsql = '"'.implode('","',$values).'"';

			//sql
			$sql = 'insert into '.$this->tableName.'('.$ksql.') values('.$vsql.')';
			// echo $sql;die;
			//执行
			$res = mysqli_query($this->link,$sql);
			//判断
			if($res){
				//返回刚插入的数据id
				return mysqli_insert_id($this->link);
			}else{
				return false;
			}
		}

		//删除操作
		public function delete($id)
		{
			//sql
			$sql = 'delete from '.$this->tableName.' where '.$this->pk.' = '.$id;
			//发送
			$res = mysqli_query($this->link,$sql);
			//判断
			if($res){
				return mysqli_affected_rows($this->link);
			}else{
				return false;
			}
		}

		//修改 update
		/*
			['username'='zshangsan','password'=>1234567,'id'=>10];
			
			'update user set username="admins",password="pass" where id = 10
		*/
		public function update(array $arr)
		{
			//拼接sql
			$sql = '';
			$where = '';
			//遍历数组
			foreach($arr as $k=>$v){
				//检测是否为主键
				if($k == $this->pk){
					$where = ' where '.$this->pk.' = '.$v;
				}else{
					$sql .= $k.' = "'.$v.'",';
				}
			}
			//去除多余的逗号
			$set_sql = rtrim($sql,',');
			//sql
			$sql = 'update '.$this->tableName.' set '.$set_sql.$where;
			//发送
			$res = mysqli_query($this->link,$sql);
			//判断
			if($res){
				return mysqli_affected_rows($this->link);
			}else{
				return false;
			}
		}

		//单条查询
		public function find($id)
		{
			//sql
			$sql = 'select * from '.$this->tableName.' where '.$this->pk.'  = '.$id;
			//发送 执行sql
			$res = mysqli_query($this->link,$sql);
			//获取结果
			if($res){
				return mysqli_fetch_assoc($res);
			}
		}

		//多条查询
		public function select()
		{
			//检测是否有where条件
			$w = '';
			if(!empty($this->where)){
				$w = ' where '.$this->where;
			}

			//检测limit
			$l = '';
			if(!empty($this->limit)){
				$l = ' limit '.$this->limit;
			}

			//检测字段条件
			$f = '*';
			if(!empty($this->field)){
				$f = $this->field;
			}

			//清空指令  清空条件中的值
			$this->where = '';
			$this->limit = '';
			$this->field = '';

			//sql
			$sql = 'select '.$f.' from '.$this->tableName.$w.$l;
			// echo $sql;
			//发送
			$res = mysqli_query($this->link,$sql);
			//检测结果
			if($res){
				//定义空数组
				$arr = [];
				//遍历
				while($row = mysqli_fetch_assoc($res)){
					$arr[] = $row;
				}
				//返回数据
				return $arr;
			}else{
				return false;
			}
		}

		//where条件 方法
		public function where($w)
		{
			//把参数存到成员属性中
			$this->where = $w;
			//返回对象
			return $this;
		}

		//limit条件
		public function limit($l)
		{
			//把参数存到成员属性中
			$this->limit = $l;
			//返回对象
			return $this;
		}

		//field 条件
		public function field($f)
		{
			//把参数存到成员属性中
			$this->field = $f;
			//返回对象
			return $this;
		}

		//统计表中的所有数据
		public function count()
		{
			//检测where条件
			$w = '';
			if(!empty($this->where)){
				$w = ' where '.$this->where;
			}

			//sql
			$sql = 'select count(*) as total from '.$this->tableName.$w;

			$res = mysqli_query($this->link,$sql);
			if($res){
				$result = mysqli_fetch_assoc($res);
				return $result['total'];
			}else{
				return false;
			}
		}

	}

	//实例化对象
	$user = new Model('user');

	//查询单条
	// $res = $user->find(2);

	//查询多条
	// $res = $user->select();

	//添加操作
		// $arr = [
		// 		'username'=>'zhangsan',
		// 		'password'=>'zhangsans',
		// 		'email'=>'zhangsan@qq.com',
		// 		'sex' => 0
		// 		];
		
		// $res = $user->insert($arr);
		// var_dump($res);

	//删除操作
		// $res = $user->delete(7);
		// var_dump($res);

	$arr = [
			'username'=>'111',
			'password'=>'111',
			'email'=>'zhangsan@qq.com',
			'id'=>6
			];
	//修改操作
		// $res = $user->update($arr);
	
	//多条查询

	//连贯操作
		// $res = $user->where('id < 5')->select();
		// $res = $user->where('id < 5')->limit('10,10')->select();
		// $res = $user->field('id,username,email')->where('id < 5')->limit('2')->select();
		$res = $user->field('id,username,email')->where('id < 5')->select();
		// var_dump($res);

		// $res = $user->select();
		// var_dump($res);

	//统计操作
		$res = $user->where('id<5')->count();

		// var_dump($res);
	// $user->getFileds();










 ?>